{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "import datetime\n",
    "import pytz\n",
    "import requests\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2021-03-11 08:38:34.571407-05:00\n"
     ]
    }
   ],
   "source": [
    "tz = pytz.timezone('America/New_York')\n",
    "a = datetime.datetime.now(tz)\n",
    "#a = datetime.datetime.now(tz).strftime(\"%Y-%m-%d %H:%M:%S\")\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2021, 3, 11, 8, 41, 54, 211304, tzinfo=<DstTzInfo 'America/New_York' EST-1 day, 19:00:00 STD>)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = datetime.datetime.now(tz)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2021-03-11 08:42'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#time.time()\n",
    "\n",
    "\n",
    "a = datetime.datetime.now(tz).strftime(\"%Y-%m-%d %H:%M\")\n",
    "\n",
    "#getsynal   B   S\n",
    "\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "tz = pytz.timezone('America/New_York')\n",
    "now = datetime.datetime.now(tz)\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "h %s m s% 8 48\n"
     ]
    }
   ],
   "source": [
    "print('h %s m s%',now.hour, now.minute)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    " if now.hour == 15 and now.minute == 55:\n",
    "    logging.info('place order:')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "dingurl = 'https://oapi.dingtalk.com/robot/send?access_token=e1cf98777a780626449413a002d893d817abb9b46cc17fc9f038ec4698eafa8a'\n",
    "headers = {\"Content-Type\": \"application/json\"}\n",
    "\n",
    "dingurl_xiaoce='https://oapi.dingtalk.com/robot/send?access_token=a9e173459658eef1997dafa248d806d7e25b32b3ca7603058f9084104b8e454b'\n",
    "\n",
    "myding = \"https://oapi.dingtalk.com/robot/send?access_token=82bc909d7890a6a62dc2a250df8eea25d6666b1360a79de0d61b08483774fb36\"#  自己的顶顶群"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "# 1\n",
    "requests.post(url, data=json.dumps(data))\n",
    "# 2-json参数会自动将字典类型的对象转换为json格式\n",
    "requests.post(url, json=data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "jsonStr = {\n",
    "     \"msgtype\": \"text\",\n",
    "     \"text\": {\n",
    "         \"content\": \"[strategy]  trade\"\n",
    "     },\n",
    "     \"at\": {\n",
    "         \"atMobiles\": [\n",
    "             \"15168412235\"\n",
    "         ], \n",
    "         \"isAtAll\": True\n",
    "     }\n",
    " }\n",
    "\n",
    "#https://developers.dingtalk.com/document/app/custom-robot-access"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "jsonStr = {\n",
    "     \"msgtype\": \"text\",\n",
    "     \"text\": {\n",
    "         \"content\": \"[Artstrategy] 启动\"\n",
    "     }\n",
    " }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'errcode': 0, 'errmsg': 'ok'}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res = requests.post(dingurl_xiaoce, json=jsonStr, headers=headers)\n",
    "res.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'errcode': 0, 'errmsg': 'ok'}"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res = requests.post(dingurl, data=json.dumps(jsonStr), headers=headers)\n",
    "res.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [],
   "source": [
    "requests.post?\n",
    "\n",
    "#res = json.loads(jsonStr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "module 'json' has no attribute 'keys'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-57-d1756c56e8c2>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mjson\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mkeys\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m  \u001b[0;31m#打印字典的所有key\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m: module 'json' has no attribute 'keys'"
     ]
    }
   ],
   "source": [
    "print(json.keys())  #打印字典的所有key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'msgtype': 'text',\n",
       " 'text': {'content': '我就是我, @15168412235 是不一样的烟火'},\n",
       " 'at': {'atMobiles': ['15168412235'], 'isAtAll': False}}"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "jsonStr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'errcode': 310000,\n",
       " 'errmsg': 'keywords not in content, more: [https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq]'}"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 消息主体\n",
    "data =  {\n",
    "\"msgtype\": \"text\",\n",
    " \"text\": {\n",
    "     \"content\": \"自定义机器人消息推送测试\"\n",
    " }\n",
    "}\n",
    "\n",
    "# 使用post请求推送消息\n",
    "res = requests.post(dingurl, data=json.dumps(data), headers=headers)\n",
    "res.json()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
